2000W / 48V LLC Battery-Charger  V1.00
xmc_i2s.c
Go to the documentation of this file.
1 
70 /*********************************************************************************************************************
71  * HEADER FILES
72  ********************************************************************************************************************/
73 
74 #include <xmc_scu.h>
75 #include <xmc_i2s.h>
76 
77 /*********************************************************************************************************************
78  * MACROS
79  ********************************************************************************************************************/
80 /* To take into account the SCLK divider by 2 and the PPPEN divider (see Divider Mode Counter figure in RM) */
81 #define XMC_I2S_CH_OVERSAMPLING (4UL)
82 
83 /*********************************************************************************************************************
84  * API IMPLEMENTATION
85  ********************************************************************************************************************/
86 
87 /* Initializes the selected I2S channel with the config structure. */
88 void XMC_I2S_CH_Init(XMC_USIC_CH_t *const channel, const XMC_I2S_CH_CONFIG_t *const config)
89 {
90  XMC_ASSERT("XMC_I2S_CH_Init: data_delayed_sclk_periods value not valid",(config->data_delayed_sclk_periods > 0U) &&
91  (config->data_delayed_sclk_periods < config->frame_length));
92  XMC_USIC_CH_Enable(channel);
93 
95  {
96  /* Configure baud rate */
97  (void)XMC_I2S_CH_SetBaudrate(channel, config->baudrate);
98  }
99  /* Configuration of USIC Shift Control */
100  /* Transmission Mode (TRM) = 1 */
101  channel->SCTR = (uint32_t)(0x3UL << USIC_CH_SCTR_TRM_Pos) |
102  (uint32_t)((config->frame_length -1U) << USIC_CH_SCTR_FLE_Pos) |
103  (uint32_t)((config->data_bits - 1U) << USIC_CH_SCTR_WLE_Pos) |
105 
106  /* Configuration of USIC Transmit Control/Status Register */
107  /* TBUF Data Enable (TDEN) = 1 */
108  /* TBUF Data Single Shot Mode (TDSSM) = 1 */
109  /* WA mode enabled(WAMD) = 1 */
110  channel->TCSR = (uint32_t)((channel->TCSR & (~(USIC_CH_TCSR_WLEMD_Msk |
115  (0x01UL << USIC_CH_TCSR_TDEN_Pos) |
117 
118  if(config->bus_mode == XMC_I2S_CH_BUS_MODE_MASTER)
119  {
120  /* Configuration of Protocol Control Register */
121  channel->PCR_IISMode = (uint32_t)USIC_CH_PCR_IISMode_WAGEN_Msk;
122  }
123 
124  /* Configuration of Protocol Control Register */
125  channel->PCR_IISMode |= (uint32_t)(USIC_CH_PCR_IISMode_DTEN_Msk |
126  (uint32_t)config->wa_inversion) |
127  ((uint32_t)((uint32_t)config->data_delayed_sclk_periods - 1U) << USIC_CH_PCR_IISMode_TDEL_Pos);
128 
130 
131  /* Clear protocol status */
132  channel->PSCR = 0xFFFFFFFFUL;
133 }
134 
135 
136 XMC_I2S_CH_STATUS_t XMC_I2S_CH_SetBaudrate(XMC_USIC_CH_t *const channel, const uint32_t rate)
137 {
138  XMC_I2S_CH_STATUS_t status;
139 
140  status = XMC_I2S_CH_STATUS_ERROR;
141 
142  if (rate <= (XMC_SCU_CLOCK_GetPeripheralClockFrequency() >> 1U))
143  {
145  {
146  channel->BRG = (uint32_t)((channel->BRG & ~(USIC_CH_BRG_CTQSEL_Msk)) |
147  (0x2UL << USIC_CH_BRG_CTQSEL_Pos)) |
149 
150  status = XMC_I2S_CH_STATUS_OK;
151  }
152 
153  }
154  return status;
155 }
156 
157 void XMC_I2S_CH_SetSystemWordLength(XMC_USIC_CH_t *const channel,uint32_t sclk_cycles_system_word_length)
158 {
159  uint32_t sclk_cycles_system_word_length_temp;
160  uint8_t dctq_temp;
161  uint8_t pctq_temp;
162  uint8_t dctq = 1U;
163  uint8_t pctq = 1U;
164  uint8_t best_error = 64U;
165  uint8_t error;
166  XMC_ASSERT("XMC_I2S_CH_Init: data_delayed_sclk_periods value not valid",(sclk_cycles_system_word_length > 0U) && (sclk_cycles_system_word_length < 65U));
167 
168 
169  for (dctq_temp =1U; dctq_temp < 33U ; dctq_temp++)
170  {
171  for (pctq_temp =1U; pctq_temp < 5U ; pctq_temp++)
172  {
173  sclk_cycles_system_word_length_temp = ((uint32_t)dctq_temp) * ((uint32_t)pctq_temp);
174  if(sclk_cycles_system_word_length_temp == sclk_cycles_system_word_length)
175  {
176  dctq = dctq_temp;
177  pctq = pctq_temp;
178  break;
179  }
180  if (sclk_cycles_system_word_length_temp > sclk_cycles_system_word_length)
181  {
182  error = (uint8_t)(sclk_cycles_system_word_length_temp - sclk_cycles_system_word_length);
183  }
184  else
185  {
186  error = (uint8_t)(sclk_cycles_system_word_length - sclk_cycles_system_word_length_temp);
187  }
188 
189  if(error < best_error)
190  {
191  best_error = error;
192  dctq = dctq_temp;
193  pctq = pctq_temp;
194  }
195  }
196  }
197  channel->BRG = (uint32_t)((channel->BRG & ~((uint32_t)(USIC_CH_BRG_DCTQ_Msk |
199  (uint32_t)((uint32_t)((uint32_t)((uint32_t)dctq- 1U) << USIC_CH_BRG_DCTQ_Pos) |
200  (uint32_t)((uint32_t)((uint32_t)pctq- 1U) << USIC_CH_BRG_PCTQ_Pos)));
201 }
202 
203 /* Puts the data into FIFO if FIFO mode is enabled or else into standard buffers, by setting the proper mode. */
204 void XMC_I2S_CH_Transmit(XMC_USIC_CH_t *const channel, const uint16_t data, const XMC_I2S_CH_CHANNEL_t channel_number)
205 {
206  /* Check FIFO size */
207  if ((channel->TBCTR & USIC_CH_TBCTR_SIZE_Msk) == 0U)
208  {
209  while((uint32_t)XMC_USIC_CH_GetTransmitBufferStatus(channel) == (uint32_t)XMC_USIC_CH_TBUF_STATUS_BUSY)
210  {
211  }
212 
214 
215  channel->TBUF[(uint32_t)channel_number << 4] = data;
216  }
217  else
218  {
219  channel->IN[(uint32_t)channel_number << 4] = data;
220  }
221 }
222 
223 /* Reads the data from the buffers based on the FIFO mode selection. */
225 {
226  uint16_t retval;
227 
228  /* Check FIFO size */
229  if ((channel->RBCTR & USIC_CH_RBCTR_SIZE_Msk) == 0U)
230  {
231  retval = (uint16_t)channel->RBUF;
232  }
233  else
234  {
235  retval = (uint16_t)channel->OUTR;
236  }
237 
238  return retval;
239 }
240 
242 {
244 
245  if (((uint32_t)XMC_USIC_CH_GetTransmitBufferStatus(channel) & (uint32_t)XMC_USIC_CH_TBUF_STATUS_BUSY) != 0U)
246  {
247  status = XMC_I2S_CH_STATUS_BUSY;
248  }
249  else
250  {
251  /* USIC channel in IDLE mode */
253  }
254 
255  return status;
256 }
257 
258 void XMC_I2S_CH_EnableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
259 {
260  channel->CCR |= (event&0x1fc00U);
261  channel->PCR_IISMode |= ((event >> 2U) & 0x8070U);
262 }
263 
264 void XMC_I2S_CH_DisableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
265 {
266  channel->CCR &= (uint32_t)~(event&0x1fc00U);
267  channel->PCR_IISMode &= (uint32_t)~((event >> 2U) & 0x8070U);
268 }
uint32_t XMC_SCU_CLOCK_GetPeripheralClockFrequency(void)
Definition: xmc1_scu.c:792
void XMC_I2S_CH_SetSystemWordLength(XMC_USIC_CH_t *const channel, uint32_t sclk_cycles_system_word_length)
Definition: xmc_i2s.c:157
uint32_t baudrate
Definition: xmc_i2s.h:231
__IO uint32_t PCR_IISMode
Definition: xmc_usic.h:499
__STATIC_INLINE void XMC_USIC_CH_SetMode(XMC_USIC_CH_t *const channel, const XMC_USIC_CH_OPERATING_MODE_t mode)
Definition: xmc_usic.h:2076
__IO uint32_t TBUF[32]
Definition: xmc_usic.h:523
#define USIC_CH_BRG_CTQSEL_Msk
Definition: XMC1400.h:2820
__STATIC_INLINE XMC_USIC_CH_TBUF_STATUS_t XMC_USIC_CH_GetTransmitBufferStatus(XMC_USIC_CH_t *const channel)
Definition: xmc_usic.h:1182
#define USIC_CH_TCSR_TDEN_Pos
Definition: XMC1400.h:2993
#define USIC_CH_TCSR_WLEMD_Msk
Definition: XMC1400.h:2976
#define USIC_CH_TCSR_FLEMD_Msk
Definition: XMC1400.h:2980
__O uint32_t IN[32]
Definition: xmc_usic.h:534
I2S_CH configuration structure.
Definition: xmc_i2s.h:229
XMC_USIC_CH_STATUS_t XMC_USIC_CH_SetBaudrate(XMC_USIC_CH_t *const channel, uint32_t rate, uint32_t oversampling)
Definition: xmc_usic.c:120
#define USIC_CH_BRG_PCTQ_Pos
Definition: XMC1400.h:2821
__IO uint32_t CCR
Definition: xmc_usic.h:504
uint8_t frame_length
Definition: xmc_i2s.h:235
#define USIC_CH_TCSR_TDSSM_Msk
Definition: XMC1400.h:2992
XMC_I2S_CH_BUS_MODE_t bus_mode
Definition: xmc_i2s.h:240
uint16_t XMC_I2S_CH_GetReceivedData(XMC_USIC_CH_t *const channel)
Definition: xmc_i2s.c:224
#define XMC_I2S_CH_OVERSAMPLING
I2S driver for XMC microcontroller family.
Definition: xmc_i2s.c:81
#define XMC_ASSERT(msg, exp)
Definition: xmc_common.h:132
#define USIC_CH_TCSR_SELMD_Msk
Definition: XMC1400.h:2978
#define USIC_CH_TCSR_WAMD_Msk
Definition: XMC1400.h:2982
#define USIC_CH_SCTR_TRM_Pos
Definition: XMC1400.h:2967
#define USIC_CH_BRG_PCTQ_Msk
Definition: XMC1400.h:2822
#define USIC_CH_BRG_PPPEN_Msk
Definition: XMC1400.h:2818
uint8_t data_bits
Definition: xmc_i2s.h:232
void XMC_I2S_CH_DisableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
Definition: xmc_i2s.c:264
enum XMC_I2S_CH_STATUS XMC_I2S_CH_STATUS_t
I2S Status.
void XMC_I2S_CH_Transmit(XMC_USIC_CH_t *const channel, const uint16_t data, const XMC_I2S_CH_CHANNEL_t channel_number)
Definition: xmc_i2s.c:204
__I uint32_t RBUF
Definition: xmc_usic.h:516
#define USIC_CH_PCR_IISMode_TDEL_Pos
Definition: XMC1400.h:3177
void XMC_USIC_CH_Enable(XMC_USIC_CH_t *const channel)
Definition: xmc_usic.c:79
XMC_I2S_CH_STATUS_t XMC_I2S_CH_Stop(XMC_USIC_CH_t *const channel)
Definition: xmc_i2s.c:241
__I uint32_t OUTR
Definition: xmc_usic.h:531
#define USIC_CH_TCSR_HPCMD_Msk
Definition: XMC1400.h:2984
#define USIC_CH_BRG_CTQSEL_Pos
Definition: XMC1400.h:2819
__STATIC_INLINE void XMC_I2S_CH_ClearStatusFlag(XMC_USIC_CH_t *const channel, const uint32_t flag)
Definition: xmc_i2s.h:499
__IO uint32_t SCTR
Definition: xmc_usic.h:494
__IO uint32_t TCSR
Definition: xmc_usic.h:495
#define USIC_CH_PCR_IISMode_WAGEN_Msk
Definition: XMC1400.h:3164
#define USIC_CH_TBCTR_SIZE_Msk
Definition: XMC1400.h:3530
__IO uint32_t TBCTR
Definition: xmc_usic.h:526
__IO uint32_t RBCTR
Definition: xmc_usic.h:527
XMC_I2S_CH_STATUS_t XMC_I2S_CH_SetBaudrate(XMC_USIC_CH_t *const channel, const uint32_t rate)
Definition: xmc_i2s.c:136
#define USIC_CH_SCTR_SDIR_Msk
Definition: XMC1400.h:2958
__O uint32_t PSCR
Definition: xmc_usic.h:514
#define USIC_CH_SCTR_WLE_Pos
Definition: XMC1400.h:2971
#define USIC_CH_RBCTR_SIZE_Msk
Definition: XMC1400.h:3554
#define USIC_CH_BRG_DCTQ_Pos
Definition: XMC1400.h:2823
#define USIC_CH_SCTR_FLE_Pos
Definition: XMC1400.h:2969
uint8_t data_delayed_sclk_periods
Definition: xmc_i2s.h:238
void XMC_I2S_CH_EnableEvent(XMC_USIC_CH_t *const channel, const uint32_t event)
Definition: xmc_i2s.c:258
enum XMC_I2S_CH_CHANNEL XMC_I2S_CH_CHANNEL_t
Defines the Polarity of the WA in the SELO output lines in relation to the internal WA signal...
void XMC_I2S_CH_Init(XMC_USIC_CH_t *const channel, const XMC_I2S_CH_CONFIG_t *const config)
Definition: xmc_i2s.c:88
#define USIC_CH_PCR_IISMode_DTEN_Msk
Definition: XMC1400.h:3166
#define USIC_CH_BRG_DCTQ_Msk
Definition: XMC1400.h:2824
XMC_I2S_CH_WA_POLARITY_t wa_inversion
Definition: xmc_i2s.h:239
__IO uint32_t BRG
Definition: xmc_usic.h:491